package com.my.study.juc.basic;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;

/**
 * @author Carlos
 * @version 1.0
 * @Description TODO
 * @date 2021/3/30 22:23
 **/
@Slf4j
public class BasicThread2 extends Thread {
    private String url;
    private String fileName;

    public BasicThread2(String url, String fileName) {
        this.url = url;
        this.fileName = fileName;
    }


    @Override
    public void run() {
        WebDownloader downloader = new WebDownloader();
        downloader.downloads(url, fileName);
        System.out.println("下载成功，fileName = " + fileName);
    }

    public static void main(String[] args) {

        String path = BasicThread2.class.getClassLoader().getResource("").getPath();
        System.out.println(path);

        BasicThread2 t1 = new BasicThread2("http://www.shijuepi.com/uploads/allimg/200918/1-20091Q10417.jpg", path + "t1.jpg");
        BasicThread2 t2 = new BasicThread2("http://www.shijuepi.com/uploads/allimg/200918/1-20091Q10420.jpg", path + "t2.jpg");
        BasicThread2 t3 = new BasicThread2("http://www.wenzhousx.com/yujeu/wenzhou/1310/4_131015235912_3.jpg", path + "t3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}

@Slf4j
class WebDownloader {

    public void downloads(String url, String fileName) {
        try {
            FileUtils.copyURLToFile(new URL(url), new File(fileName));
        } catch (IOException e) {
            log.error("downloads 下载错误", e);
        }
    }

}
